{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/colby/sd_480/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py\n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "print(sys.argv[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "utf-8\n"
     ]
    }
   ],
   "source": [
    "print(sys.getdefaultencoding()) # 用于获取系统当前的编码， 一般默认为 ascii"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['/home/colby/sd_480/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py', '-f', '/run/user/1000/jupyter/kernel-6068e318-6ea6-4a19-8b25-1e00a99c8499.json']\n"
     ]
    }
   ],
   "source": [
    "print(sys.argv) # 实现 从程序外部向程序传递参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "ename": "SystemExit",
     "evalue": "0",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[0;31mSystemExit\u001b[0m\u001b[0;31m:\u001b[0m 0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/colby/sd_480/anaconda3/lib/python3.7/site-packages/IPython/core/interactiveshell.py:2969: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
      "  warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
     ]
    }
   ],
   "source": [
    "sys.exit(0) # 实现在程序的中间退出， arg=0 为正常退出"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "utf-8\n"
     ]
    }
   ],
   "source": [
    "print(sys.getfilesystemencoding()) # 用于获取文件系统使用的编码方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['', '/home/colby/sd_480/01.liang_hua_tou_zi', '/home/colby/sd_480/anaconda3/lib/python37.zip', '/home/colby/sd_480/anaconda3/lib/python3.7', '/home/colby/sd_480/anaconda3/lib/python3.7/lib-dynload', '/home/colby/sd_480/anaconda3/lib/python3.7/site-packages', '/home/colby/sd_480/anaconda3/lib/python3.7/site-packages/IPython/extensions', '/home/colby/.ipython']\n"
     ]
    }
   ],
   "source": [
    "print(sys.path) # 由于获取指定的模块搜索路径的字符串集合，可以将写好的模块放在所得到的某个路径下，这样就可以在程序中 直接 import 时，正确找到"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "linux\n"
     ]
    }
   ],
   "source": [
    "print(sys.platform) # 用于获取当前的系统平台"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>\n",
      "<ipykernel.iostream.OutStream object at 0x7f68548ba1d0>\n",
      "<ipykernel.iostream.OutStream object at 0x7f68548ba710>\n"
     ]
    }
   ],
   "source": [
    "print(sys.stdin)\n",
    "\n",
    "print(sys.stdout)\n",
    "\n",
    "print(sys.stderr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>key</th>\n",
       "      <th>data1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>a</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  key  data1\n",
       "0   b      0\n",
       "1   b      1\n",
       "2   a      2"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pandas import DataFrame\n",
    "\n",
    "df1 = DataFrame({'key':['b', 'b', 'a', 'c', 'a', 'a', 'b'], 'data1':range(7)})\n",
    "\n",
    "df1\n",
    "\n",
    "df1.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "__main__\n"
     ]
    }
   ],
   "source": [
    "print(__name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "It's main\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    print(\"It's main\")\n",
    "else:\n",
    "    print(\"It's not main\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 认识dir() 函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. 在Python中有很多模块，有时我们会忘记一个模块有哪些功能，这时可以通过dir()函数来查看指定模块的功能列表。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Categorical',\n",
       " 'CategoricalIndex',\n",
       " 'DataFrame',\n",
       " 'DateOffset',\n",
       " 'DatetimeIndex',\n",
       " 'ExcelFile',\n",
       " 'ExcelWriter',\n",
       " 'Expr',\n",
       " 'Float64Index',\n",
       " 'Grouper',\n",
       " 'HDFStore',\n",
       " 'Index',\n",
       " 'IndexSlice',\n",
       " 'Int64Index',\n",
       " 'Interval',\n",
       " 'IntervalIndex',\n",
       " 'MultiIndex',\n",
       " 'NaT',\n",
       " 'Panel',\n",
       " 'Period',\n",
       " 'PeriodIndex',\n",
       " 'RangeIndex',\n",
       " 'Series',\n",
       " 'SparseArray',\n",
       " 'SparseDataFrame',\n",
       " 'SparseSeries',\n",
       " 'Term',\n",
       " 'TimeGrouper',\n",
       " 'Timedelta',\n",
       " 'TimedeltaIndex',\n",
       " 'Timestamp',\n",
       " 'UInt64Index',\n",
       " 'WidePanel',\n",
       " '_DeprecatedModule',\n",
       " '__builtins__',\n",
       " '__cached__',\n",
       " '__doc__',\n",
       " '__docformat__',\n",
       " '__file__',\n",
       " '__loader__',\n",
       " '__name__',\n",
       " '__package__',\n",
       " '__path__',\n",
       " '__spec__',\n",
       " '__version__',\n",
       " '_hashtable',\n",
       " '_lib',\n",
       " '_libs',\n",
       " '_np_version_under1p10',\n",
       " '_np_version_under1p11',\n",
       " '_np_version_under1p12',\n",
       " '_np_version_under1p13',\n",
       " '_np_version_under1p14',\n",
       " '_np_version_under1p15',\n",
       " '_tslib',\n",
       " '_version',\n",
       " 'api',\n",
       " 'bdate_range',\n",
       " 'compat',\n",
       " 'concat',\n",
       " 'core',\n",
       " 'crosstab',\n",
       " 'cut',\n",
       " 'date_range',\n",
       " 'datetime',\n",
       " 'datetools',\n",
       " 'describe_option',\n",
       " 'errors',\n",
       " 'eval',\n",
       " 'factorize',\n",
       " 'get_dummies',\n",
       " 'get_option',\n",
       " 'get_store',\n",
       " 'groupby',\n",
       " 'infer_freq',\n",
       " 'interval_range',\n",
       " 'io',\n",
       " 'isna',\n",
       " 'isnull',\n",
       " 'json',\n",
       " 'lib',\n",
       " 'lreshape',\n",
       " 'match',\n",
       " 'melt',\n",
       " 'merge',\n",
       " 'merge_asof',\n",
       " 'merge_ordered',\n",
       " 'notna',\n",
       " 'notnull',\n",
       " 'np',\n",
       " 'offsets',\n",
       " 'option_context',\n",
       " 'options',\n",
       " 'pandas',\n",
       " 'parser',\n",
       " 'period_range',\n",
       " 'pivot',\n",
       " 'pivot_table',\n",
       " 'plot_params',\n",
       " 'plotting',\n",
       " 'pnow',\n",
       " 'qcut',\n",
       " 'read_clipboard',\n",
       " 'read_csv',\n",
       " 'read_excel',\n",
       " 'read_feather',\n",
       " 'read_fwf',\n",
       " 'read_gbq',\n",
       " 'read_hdf',\n",
       " 'read_html',\n",
       " 'read_json',\n",
       " 'read_msgpack',\n",
       " 'read_parquet',\n",
       " 'read_pickle',\n",
       " 'read_sas',\n",
       " 'read_sql',\n",
       " 'read_sql_query',\n",
       " 'read_sql_table',\n",
       " 'read_stata',\n",
       " 'read_table',\n",
       " 'reset_option',\n",
       " 'scatter_matrix',\n",
       " 'set_eng_float_format',\n",
       " 'set_option',\n",
       " 'show_versions',\n",
       " 'test',\n",
       " 'testing',\n",
       " 'timedelta_range',\n",
       " 'to_datetime',\n",
       " 'to_msgpack',\n",
       " 'to_numeric',\n",
       " 'to_pickle',\n",
       " 'to_timedelta',\n",
       " 'tools',\n",
       " 'tseries',\n",
       " 'tslib',\n",
       " 'unique',\n",
       " 'util',\n",
       " 'value_counts',\n",
       " 'wide_to_long']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas\n",
    "\n",
    "dir(pandas)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. 在Python中，通过 dir() 函数不仅能查看模块的功能列表，还能查看任意指定对象的功能列表。\n",
    "  1. 如下所示定义a为一个列表，并通过 dir(a) 查看a的功能列表："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__add__',\n",
       " '__class__',\n",
       " '__contains__',\n",
       " '__delattr__',\n",
       " '__delitem__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__getitem__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__iadd__',\n",
       " '__imul__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__iter__',\n",
       " '__le__',\n",
       " '__len__',\n",
       " '__lt__',\n",
       " '__mul__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__reversed__',\n",
       " '__rmul__',\n",
       " '__setattr__',\n",
       " '__setitem__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " 'append',\n",
       " 'clear',\n",
       " 'copy',\n",
       " 'count',\n",
       " 'extend',\n",
       " 'index',\n",
       " 'insert',\n",
       " 'pop',\n",
       " 'remove',\n",
       " 'reverse',\n",
       " 'sort']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1]\n",
    "\n",
    "dir(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
